
/*
 * 文件名：ReaderService.java.
 * 描述：
 * 创建人: 龚洪亮
 * 创建时间：2017年8月11日
 */

package com.fiberhome.excel.reader.service;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fiberhome.excel.reader.mapper.NeedMapper;
import com.fiberhome.excel.reader.pojo.Need;
import com.fiberhome.excel.reader.pojo.NeedExample;
import com.fiberhome.excel.reader.pojo.NeedExample.Criteria;


/**
 * @Description 
 * @author 龚洪亮
 * @version 2017年8月11日
 */
@Service
public class ReaderService {
	@Autowired
	private NeedMapper needMapper;
	
	
	/**
	 * @Description 
	 * @Date 2017年8月11日
	 * @return
	 * @throws IOException 
	 */
	public Integer readExcel(String path) throws IOException {
		InputStream is = new FileInputStream(path);
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);

		for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
			HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
			if (hssfSheet == null) {
				continue;
			}

			for (int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
				HSSFRow hssfRow = hssfSheet.getRow(rowNum);

				int minColIx = hssfRow.getFirstCellNum();
				int maxColIx = hssfRow.getLastCellNum();
				
					List rowList = new ArrayList();
					//开始读取行
					int id_pre = 0;
					for (int colIx = minColIx; colIx < maxColIx; colIx++) {
						HSSFCell cell = hssfRow.getCell(colIx);
						if (cell == null) {
							break;
						}
						String cellString = "";
						
						switch (cell.getCellType()) {
						
						case Cell.CELL_TYPE_STRING:
							cellString = cell.getStringCellValue();
							//首先判断是否已经存过了，如果已经存过了，就返回存过的单元格的id，如果没有存过，就返回的是-1
							Integer id = getCellId(cellString);
							if (id<0) {
								Need need  = new Need();
								need.setParentId(id_pre);
								need.setName(cellString);
								need.setIsParent(1);
								id_pre = needMapper.insert(need);
							} else {
								id_pre = id;
							}
							break;
						case Cell.CELL_TYPE_BLANK:
							Need need = new Need();
							need.setId(id_pre);
							need.setIsParent(0);
							needMapper.updateByPrimaryKeySelective(need);
							
							break;
						
						default:
							break;
						}
					}
				}
			}
		return 1;
		
	}
	
	
	
	public Integer insert(Need need) {
		return needMapper.insert(need);
	}
	
	/**
	 * @Description 是否存在，如果存在返回已经插入过的id 如果不存在则返回-1
	 * @Date 2017年8月11日
	 * @param name
	 * @return
	 */
	public Integer getCellId(String name) {
		Integer id = -1;
		NeedExample example = new NeedExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(name);
		List<Need> result = needMapper.selectByExample(example);
		if (result.size() == 0) {
			return id;
		}
		id = result.get(0).getId();
		return id;
	}
	
	/**
	 * @Description 更新上一个单元格的is_parent为0
	 * @Date 2017年8月11日
	 * @param id
	 * @return
	 */
	public Integer updateIsParent(Integer id) {
		Need need = new Need();
		need.setId(id);
		need.setIsParent(0);
		return needMapper.updateByPrimaryKeySelective(need);
	}
}
